package banner;

import java.util.ArrayList;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.ParallelTransition;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

// 轮播图( 缩放(3D效果,还解决了图片的上下位置)和位移(动画) )
public class Banner extends Application {
	private Pane root = new Pane();
	private SubScene ss;			// 子场景
	private Pane pane = new Pane();		// 子面板
	private HBox hBox = new HBox(10);	// 图片指示器面板
	private ImageView closeButton;	// 关闭按钮图片
	private ArrayList<ImageView> images = new ArrayList<>();	// 图片数组
	private static final double PANE_WIDTH = 800;		// 3D效果子面板的宽高
	private static final double PANE_HEIGHT= 300;
	private Timeline imagePlayer;	// 轮播图片
	private PauseTransition pause;	// 选择图片后暂停
	private int count = 0;		// 用于选择位移的图片
	private double x1 = -100;
	private double x2 = 175;
	private double x3 = 625;
	private double z1 = 100;
	private double z2 = 0;
	private double z3 = 100;
	private double initX;		// 用于拖拽面板时记录距离差
	private double initY;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		root.setBackground(new Background(new BackgroundFill(Color.DARKCYAN, new CornerRadii(20), new Insets(0))));
		
		// 加入关闭按钮图片
		closeButton = new ImageView("image/关闭按钮.png");
		
		// 加入图片
		images.add(new ImageView("image/0.jpg"));
		images.add(new ImageView("image/1.jpg"));
		images.add(new ImageView("image/2.jpg"));
		images.add(new ImageView("image/3.jpg"));
		images.add(new ImageView("image/4.jpg"));
		images.add(new ImageView("image/5.jpg"));
		images.add(new ImageView("image/6.jpg"));
		
		for(int i=3; i<images.size(); i++) {
			ImageView temp = images.get(i);
			temp.setFitWidth(450);
			temp.setFitHeight(PANE_HEIGHT);
			temp.setVisible(false);
			temp.setTranslateX(x3);
			temp.setTranslateZ(z3);
		}
		
		images.get(0).setFitWidth(450);
		images.get(0).setFitHeight(PANE_HEIGHT);
		images.get(0).setTranslateX(x1);
		images.get(0).setTranslateZ(z1);	// z轴只能位移
		
		images.get(1).setFitWidth(450);
		images.get(1).setFitHeight(PANE_HEIGHT);
		images.get(1).setTranslateX(x2);
		images.get(1).setTranslateZ(z2);
		
		images.get(2).setFitWidth(450);
		images.get(2).setFitHeight(PANE_HEIGHT);
		images.get(2).setTranslateX(x3);
		images.get(2).setTranslateZ(z3);
		pane.getChildren().addAll(images);		// 加入变量或数组
		
		// 加入图片指示器
		hBox.setAlignment(Pos.CENTER);
		
		for(int i=0; i<images.size(); i++) {
			Circle circle;
			if(i == 1) {
				circle = new Circle(6.5, Color.DEEPSKYBLUE);
			} else {
				circle = new Circle(5, Color.ANTIQUEWHITE);
			}
			circle.setCursor(Cursor.CLOSED_HAND);
			circle.setPickOnBounds(true);
			hBox.getChildren().add(circle);
		}
		hBox.setTranslateX(PANE_WIDTH/2 - hBox.prefWidth(-1)/2);
		hBox.setTranslateY(283);
		pane.getChildren().addAll(hBox);
		
		ss = new SubScene(pane, PANE_WIDTH, PANE_HEIGHT, true, SceneAntialiasing.BALANCED);
		ss.setCamera(new PerspectiveCamera());
		ss.setTranslateX(100);
		ss.setTranslateY(50);
		root.getChildren().addAll(closeButton, ss);
		  
	    Scene scene = new Scene(root, 1000, 600, Color.TRANSPARENT);
	    stage.setScene(scene);
	    stage.initStyle(StageStyle.TRANSPARENT);
		stage.show();
		
		// 事件
		// 图片指示器事件(两种遍历方式)
		for(int i=0; i<hBox.getChildren().size(); i++) {
			Node node = hBox.getChildren().get(i);
			node.setOnMouseClicked(e -> {
				int index = hBox.getChildren().indexOf(node);	// 获得下标
				setIndicatorStyle(index);
				switch(index) {
					case 0: count = 0; 	toRight(); imagePlayer.pause(); pause.play(); break;
					case 1: count = 36; toRight(); imagePlayer.pause(); pause.play(); break;
					case 2: count = 30; toRight(); imagePlayer.pause(); pause.play(); break;
					case 3: count = 24; toRight(); imagePlayer.pause(); pause.play(); break;
					case 4: count = 18; toRight(); imagePlayer.pause(); pause.play(); break;
					case 5: count = 12; toRight(); imagePlayer.pause(); pause.play(); break;
					case 6: count = 6; 	toRight(); imagePlayer.pause(); pause.play(); break;
				}
			});
		}
		
		// 轮播图片
		imagePlayer = new Timeline(new KeyFrame(Duration.seconds(3), e -> {
			toRight();
		}));
		imagePlayer.setCycleCount(Timeline.INDEFINITE);
		imagePlayer.play();
		
		// 暂停(使图片暂停轮播)
		pause = new PauseTransition(Duration.seconds(2));
		pause.setOnFinished(e -> imagePlayer.play() );
		
		// 拖拽面板
		root.setOnMousePressed(e -> {
			initX = e.getScreenX() - stage.getX();
			initY = e.getScreenY() - stage.getY();
		});
		root.setOnMouseDragged(e -> {
			stage.setX(e.getScreenX() - initX);
			stage.setY(e.getScreenY() - initY);
		});
		
		// 关闭窗口
		closeButton.setOnMouseClicked(e -> stage.close() );
	}
	
	// 图片向右位移
	public void toRight() {
		int size = images.size();
		int select1 = count % size;			// 选择图片
		int select2 = (count+1) % size;
		int select3 = (count + (size-1)) % size;
		for(int i=0; i<size; i++) {
			if(i == select1 || i == select2 || i == select3) {
				images.get(i).setVisible(true);
			} else {
				images.get(i).setVisible(false);
			}
		}
		
		// 位移加褪色
		TranslateTransition translate1 = translate(0.4, images.get(select1), x2, z2, 1);
		FadeTransition fade1 = fade(0.4, images.get(select1), 0.9, 1, 1);
		setIndicatorStyle(select1);
		
		TranslateTransition translate2 = translate(0.4, images.get(select2), x3, z3, 1);
		FadeTransition fade2 = fade(0.4, images.get(select2), 0.7, 1, 1);
		
		TranslateTransition translate3 = translate(0.4, images.get(select3), x1, z1, 1);
		FadeTransition fade3 = fade(0.4, images.get(select3), 0.7, 1, 1);
		
		ParallelTransition parallel = new ParallelTransition(translate1, fade1, translate2, fade2, translate3, fade3);
		parallel.play();
		
		count += size-1;
	}
	
	// 位移
	public TranslateTransition translate(double cycleTime, Node node, double x, double z, int cycleCount) {
		TranslateTransition tt = new TranslateTransition(Duration.seconds(cycleTime), node);
		tt.setToX(x);
		tt.setToZ(z);
		tt.setCycleCount(cycleCount);
		return tt;
	}
	
	// 褪色
	public FadeTransition fade(double cycleTime, Node node, double startValue, double endValue, int cycleCount) {
		FadeTransition ft = new FadeTransition(Duration.seconds(cycleTime), node);
		ft.setFromValue(startValue);
		ft.setToValue(endValue);
		ft.setCycleCount(cycleCount);
		return ft;
	}
	
	// 设置中央图片的指示器样式
	public void setIndicatorStyle(int center) {
		for(int i=0; i<hBox.getChildren().size(); i++) {
			Circle circle = (Circle) hBox.getChildren().get(i);
			if(i == center) {
				circle.setRadius(6.5);
				circle.setFill(Color.DODGERBLUE);
			} else {
				circle.setRadius(5);
				circle.setFill(Color.ANTIQUEWHITE);
			}
		}
	}
}
